﻿// /////////////////////////////////// 
//  
//  
//      WiseLink CCF2.0 
//     (c)Copyright 2001-2010 WiseLink Xiaojun (David) Chen 
//        Authored and Owned Xiaojun (David) Chen 
//        All Right Reserved
//  
// /////////////////////////////////// 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZICModel.Common;
using Infrastructure.Log;
using Infrastructure.Extension;

namespace ZICModel.Fault
{
    public class ZISFaultHandler : ZICBaseModel
    {
        public void GetAllFault()
        {

            int count = 0;
            long lastProcessedFaultId = zic2Entities.WiseLinkFaults.Max(t => t.SrcFaultID) ?? 0;

            foreach (GpsOutEquipmentHistory[] srcFaultChunk in zic2Entities.GpsOutEquipmentHistories.Where(
                                                                                            f => f.EquipmentId.Equals("12") &&
                                                                                            f.id > lastProcessedFaultId)
                                                                                            .OrderBy(f => f.id)
                                                                                            .QueryChunksOfSize(500))
            {

                var query = from src in srcFaultChunk.AsParallel()
                            select src;
                query.ForAll(src =>
                    {

                        FaultBase fault = new FaultBase(src);
                        HeaderHandler headerHandler = new HeaderHandler(fault);

                        headerHandler.Process();

                        if (fault.FaultHeader.IsValid)
                        {
                            BodyHandler bodyHandler = new BodyHandler(fault);
                            bodyHandler.Process();
                            SaveFault(fault);
                            count++;
                        }

                    });

                zic2Entities.SaveChanges();
                Console.WriteLine(count);
            }


        }



        public void GetUrgentByLastQueryTime(DateTime lastQueryTime)
        {
            DateTime currentTime = DateTime.Now;
            IEnumerable<Urgent> urgentList1 = from u in zic2Entities.gpsrecerealtime_buff1
                                              where u.Reserved == 1154 && u.gpstime > lastQueryTime && u.gpstime < currentTime
                                              select new Urgent { CarStatus = u.carStatu, GPSTime = u.gpstime, ReceiveTime = currentTime, Idc = u.telephone, SrcFaultId = u.id, Longitude = u.Latitude, Latitude = u.Latitude };

            IEnumerable<Urgent> urgentList2 = from u in zic2Entities.gpsrecerealtime_buff2
                                              where u.Reserved == 1154 && u.gpstime > lastQueryTime && u.gpstime < currentTime
                                              select new Urgent { CarStatus = u.carStatu, GPSTime = u.gpstime, ReceiveTime = currentTime, Idc = u.telephone, SrcFaultId = u.id, Longitude = u.Latitude, Latitude = u.Latitude };


            CallLogger.LogMessage(typeof(ZISFaultHandler), string.Format(" Buff1 record count={0},Buff2 record count={1}", urgentList1.Count(), urgentList2.Count()));
            foreach (Urgent[] srcUrgentList in urgentList1.Concat(urgentList2).AsQueryable().QueryChunksOfSize(500))
            {
                foreach (Urgent srcUrgent in srcUrgentList)
                {
                    if (srcUrgent.CarStatus != null)
                    {

                        string converted = Convert.ToString((int)srcUrgent.CarStatus, 2);
                        converted = converted.PadLeft(32 - converted.Length, '0');
                        bool isUrgent = false;
                        if (converted[9] == '1')
                        {
                            isUrgent = true;
                        }

                        CallLogger.LogMessage(typeof(ZISFaultHandler), string.Format("ZIS Crash Message AnchiId={0} GPSTime={1} SrcCarStatus={2} Converted={3} IsUrgent={4} \r\n",
                                                                            srcUrgent.SrcFaultId,
                                                                            string.Format("{0: YYYY-MM-DD HH-mm-ss}", srcUrgent.GPSTime),
                                                                            srcUrgent.CarStatus,
                                                                            converted,
                                                                            isUrgent));
                        if (isUrgent)
                        {
                            SaveUrgent(srcUrgent);
                        }
                    }
                }
                zic2Entities.SaveChanges();
            }
        }
    }
}
